package org.libsdl.app;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

/**
 * Created by Administrator on 2017/10/5 0005.
 */


public class SecondActivity extends Activity {

    private static final String TAG = "CTRL";

    //设置类ButtonListener实现接口,OnClickListener,在其中可以指定不同id的button对应不同的点击事件
//可以借此将代码抽出来，提高代码的可阅读性
    private class ButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_open:
                {
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("video/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
                    //intent.setType("video/*;image/*");//同时选择视频和图片
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    startActivityForResult(intent,1);
                }
                break;
//                    Intent intent =getIntent();
//                    //这里使用bundle绷带来传输数据
//                    Bundle bundle =new Bundle();
//                    //传输的内容仍然是键值对的形式
//                    bundle.putString("second","input.mpg");//回发的消息,hello world from secondActivity!
//                    intent.putExtras(bundle);
//                    setResult(RESULT_OK,intent);
//                    finish();
//                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        Button button= (Button) findViewById(R.id.btn_open);
        button.setOnClickListener(
                new ButtonListener()
        );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {//是否选择，没选择就不会继续
            Uri uri = data.getData();//得到uri，后面就是将uri转化成file的过程。
            String strPath = uri.getPath().toString();
            Toast.makeText(this, "文件路径："+strPath, Toast.LENGTH_SHORT).show();

            Intent intent =getIntent();
            //这里使用bundle绷带来传输数据
            Bundle bundle =new Bundle();
            //传输的内容仍然是键值对的形式
            bundle.putString("second",strPath);//回发的消息,hello world from secondActivity!
            intent.putExtras(bundle);
            setResult(RESULT_OK,intent);
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(TAG,"onKeyDown  "+keyCode);
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        Log.d(TAG,"onBackPressed");

        Intent intent =getIntent();
        //这里使用bundle绷带来传输数据
        Bundle bundle =new Bundle();
        //传输的内容仍然是键值对的形式
        bundle.putString("second","exit");//回发的消息,hello world from secondActivity!
        intent.putExtras(bundle);
        setResult(RESULT_OK,intent);
        finish();

        super.onBackPressed();
    }
}
